var evn_Path = getPath();
function getPath() {
	var envPath = document.location.pathname;
	var protoStr = document.location.protocol;
	var hostStr = document.location.host;
	var portStr = document.location.port;

	var envPathArray = envPath.split('/');
	return protoStr + "//" + hostStr + "/" + envPathArray[1] + "/";
}
function backHome(updown){
	if(updown=='down'){
		$(".home").css("background-image","url('"+evn_Path+"resource/home_press.png')");
	}else if(updown=='up'){
		var url = "main.do?mainSceneId="+mainSceneId+"&currentSceneId="+sceneSceneId+"&sceneIsOpen="+sceneIsOpen+"&mainSceneTime="+mainSceneTime+"&sceneTime="+mainSceneTime;
		window.location.href=url;
  		
	}
}
function showMainLeftTime(){
	setInterval(function(){
		var today = new Date();
	    var x = new Array("星期日", "星期一", "星期二");
	    var x = x.concat("星期三","星期四", "星期五");
	    var x = x.concat("星期六");
	    $('#mainLeftTime').html(today.format("yyyy年MM月dd日 hh:mm:ss")+' '+x[today.getDay()]);
		
	},1000);
	
}


function sceneAll(type){
	//让场景处于不被选中状态
	$(".main_right_scene ul li").attr("className","main_right_scene_ul_li");
	$(".main_open_scene").text("无场景");
	$('.main_left_menu_openClose ul li img').attr("src",evn_Path+"resource/main_left_menu_close.png");
	
	$.get(evn_Path+"main!openCloseAll.do?openClose="+type,function(data){
		
		$('#'+type+'Img').attr("src",evn_Path+"resource/main_left_menu_open.png");
	})
}

function initScene(){
	
	$(".main_left_menu ul li").click(
			function(){
					$(".main_left_menu ul li").attr("class","main_left_menu_ul_li");
					$(this).attr("class","main_left_menu_ul_li_press");
					$("#mainLeftMenuImgScene").attr("class","main_left_menu_img main_left_menu_img_scene");
					$("#mainLeftMenuImgDevice").attr("class","main_left_menu_img main_left_menu_img_device");
					$("#mainLeftMenuImgSetting").attr("class","main_left_menu_img main_left_menu_img_setting");
					var index = $(".main_left_menu ul li").index(this);
					switch(index){
						case 0:
							$(this).find("div").attr("class","main_left_menu_img_press main_left_menu_img_scene_press");
							$("#mainRight").load(evn_Path+"scene.do?sceneSceneId="+mainSceneId);
							break;
						case 1:
							$(this).find("div").attr("class","main_left_menu_img_press main_left_menu_img_device_press");
							$("#mainRight").load(evn_Path+"device.do");
							break;
						case 2:
							$(this).find("div").attr("class","main_left_menu_img_press main_left_menu_img_setting_press");
							$("#mainRight").load(evn_Path+"setting.do");
							break;
					}
					
					
				}
	  		);
	
}

function sceneOpen(sceneId){
	$(".main_right_scene ul li").attr("className","main_right_scene_ul_li");
	$("#main_right_scene_ul_li"+sceneId).attr("className","main_right_scene_ul_li_press");
	//全开全关背景图片改变
	$('.main_left_menu_openClose ul li img').attr("src",evn_Path+"resource/main_left_menu_close.png");
	$.post(evn_Path+"main!openScene.do",{sceneId:sceneId},
			function(){
				var testDate = new Date();
				 var testStr = testDate.format("yyyy年MM月dd日hh时mm分");
				$(".main_open_scene").text($("#main_right_scene_ul_li"+sceneId).text().trim()+"  "+testStr+"开启");
				mainSceneId = sceneId;
				mainSceneTime = testDate.format("yyyy-MM-dd-hh-mm");
			});
}

function sceneUsePress(sceneId){
	$(".scene_use_img_left_list ul li").attr("className","scene_use_img_left_list_ul_li");
	$("#scene_use_img_left_list_ul_li"+sceneId).attr("className","scene_use_img_left_list_ul_li_press");
	//全开全关背景图片改变
	$('.main_left_menu_openClose ul li img').attr("src",evn_Path+"resource/main_left_menu_close.png");
	currentSceneId = sceneId;
	
	$(".scene_use_img_right_device").load(evn_Path+"scene!deviceList.do?sceneId="+sceneId);
}


function initTab(){
	$(".setting_button ul li").click(
			function(){
					$(".setting_button ul li").attr("class","setting_button_ul_li");
					$(this).attr("class","setting_button_ul_li_press");
					var index = $(".setting_button ul li").index(this);
					switch(index){
						case 0:
						
							break;
						case 1:
							
							break;
						case 2:
							break;
					}
					
					
				}
	  		);
	
}

function scenePage(page,upDown,leftRight){
	if(upDown=='up'){
		$("#main_scene").load(evn_Path+"main!scene.do?currentPage="+page);
	}else if(upDown=='down'){
		if(leftRight=='left'){
			$(".main_arrow_left").css("background-image","url('"+evn_Path+"resource/main_arrow_left_press.png')");
		}else if(leftRight=='right'){
			$(".main_arrow_right").css("background-image","url('"+evn_Path+"resource/main_arrow_right_press.png')");
		}
	}
	
}

function queryDevice(upDown){
	if(upDown=='down'){
		$(".main_right_device_button").css("background-image","url('"+evn_Path+"resource/main_right_query_press.png')");
		
	}else if(upDown=='up'){
		$("#main_device").load(evn_Path+"main!openDevice.do",function(data){
			$(".main_right_device_button").css("background-image","url('"+evn_Path+"resource/main_right_query.png')");
		});
	}
	
}

function listOpenUp(currentPage,currentPageName,listSize,pageSize,buttonClass,buttonImg,buttonPressImg,liId,upDown){
	
	if(currentPage == 1) return ;
	if(upDown=='down'){
		$("."+buttonClass).css("background-image","url('"+evn_Path+"resource/"+buttonPressImg+"')");
	}else if(upDown=='up'){
		currentPage-=1;
		eval(currentPageName+'='+currentPage);
		for(var i=1;i<=listSize;i++){
			var startId = (currentPage-1) * pageSize ;
			var endId = (currentPage-1) * pageSize + pageSize;
			if(i > startId && i <= endId){
				$("#"+liId+i).css("display","block");
			}else{
				$("#"+liId+i).css("display","none");
			}
		}
		$("."+buttonClass).css("background-image","url('"+evn_Path+"resource/"+buttonImg+"')");
	}
}

function listOpenDown(currentPage,currentPageName,listSize,pageSize,buttonClass,buttonImg,buttonPressImg,liId,upDown){
	var totalPage =  Math.ceil(listSize/pageSize);
	totalPage = (totalPage==0?1:totalPage);
	if(currentPage == totalPage) return ;
	if(upDown=='down'){
		$("."+buttonClass).css("background-image","url('"+evn_Path+"resource/"+buttonPressImg+"')");
	}else if(upDown=='up'){
		currentPage+=1;
		eval(currentPageName+'='+currentPage);
		for(var i=1;i<=listSize;i++){
			var startId = (currentPage-1) * pageSize ;
			var endId = (currentPage-1) * pageSize + pageSize;
			if(i > startId && i <= endId){
				$("#"+liId+i).css("display","block");
			}else{
				$("#"+liId+i).css("display","none");
			}
		}
		$("."+buttonClass).css("background-image","url('"+evn_Path+"resource/"+buttonImg+"')");
	}
}

function pressDevice(deviceId,i,deviceType,listSpace){
	currentDeviceId = deviceId;
	if($("#deviceName")!=null){
		$("#deviceName").val($("#device_use_img_li"+i).text());
		$("#deviceType").val(deviceType);
	}
	if(currentDevice!=0){
		$("#device_use_img_li"+currentDevice).attr("className",currentDeviceClass);
	}
	currentDeviceClass = $("#device_use_img_li"+i).attr("className");
	currentDevice = i;
	pressDeviceType = deviceType;
	if(deviceType=='1'){
		if(listSpace=='space'){
			$("#device_use_img_li"+i).attr("className","device_use_img_li_on_off_press_s");
		}else{
			$("#device_use_img_li"+i).attr("className","device_use_img_li_on_off_press");
		}
		$(".device_use_content_bottom_level").css("display","none");
	}else if(deviceType=='2'){
		if(listSpace=='space'){
			$("#device_use_img_li"+i).attr("className","device_use_img_li_turn_press_s");
		}else{
			
			$("#device_use_img_li"+i).attr("className","device_use_img_li_turn_press");
		}
		$(".device_use_content_bottom_level").css("display","block");
	}else if(deviceType=='3'){
		if(listSpace=='space'){
			$("#device_use_img_li"+i).attr("className","device_use_img_li_socket_press_s");
		}else{
			$("#device_use_img_li"+i).attr("className","device_use_img_li_socket_press");
		}
		$(".device_use_content_bottom_level").css("display","none");
	}
	$.getJSON(evn_Path+"device!queryDevice.do?deviceId="+deviceId,null,
			function(deviceObj){
				levelChange(deviceObj.level==null?0:deviceObj.level);
			});
}

function levelChange(levelChange){
	
	switch(levelChange){
	case 0:{
		$(".device_use_content_bottom_level_1").css("background-image","");
		$(".device_use_content_bottom_level_2").css("background-image","");
		$(".device_use_content_bottom_level_3").css("background-image","");
		$(".device_use_content_bottom_level_4").css("background-image","");
		$(".device_use_content_bottom_level_5").css("background-image","");
		$(".device_use_content_bottom_level_6").css("background-image","");
		$(".device_use_content_bottom_level_7").css("background-image","");
		break;
	}
	case 1:{
		$(".device_use_content_bottom_level_1").css("background-image","url('"+evn_Path+"resource/device_level_1.png')");
		$(".device_use_content_bottom_level_2").css("background-image","");
		$(".device_use_content_bottom_level_3").css("background-image","");
		$(".device_use_content_bottom_level_4").css("background-image","");
		$(".device_use_content_bottom_level_5").css("background-image","");
		$(".device_use_content_bottom_level_6").css("background-image","");
		$(".device_use_content_bottom_level_7").css("background-image","");
		break;
	}
	case 2:{
		$(".device_use_content_bottom_level_1").css("background-image","url('"+evn_Path+"resource/device_level_1.png')");
		$(".device_use_content_bottom_level_2").css("background-image","url('"+evn_Path+"resource/device_level_2.png')");
		$(".device_use_content_bottom_level_3").css("background-image","");
		$(".device_use_content_bottom_level_4").css("background-image","");
		$(".device_use_content_bottom_level_5").css("background-image","");
		$(".device_use_content_bottom_level_6").css("background-image","");
		$(".device_use_content_bottom_level_7").css("background-image","");
		break;
	}
	case 3:{
		$(".device_use_content_bottom_level_1").css("background-image","url('"+evn_Path+"resource/device_level_1.png')");
		$(".device_use_content_bottom_level_2").css("background-image","url('"+evn_Path+"resource/device_level_2.png')");
		$(".device_use_content_bottom_level_3").css("background-image","url('"+evn_Path+"resource/device_level_3.png')");
		$(".device_use_content_bottom_level_4").css("background-image","");
		$(".device_use_content_bottom_level_5").css("background-image","");
		$(".device_use_content_bottom_level_6").css("background-image","");
		$(".device_use_content_bottom_level_7").css("background-image","");
		break;
	}
	case 4:{
		$(".device_use_content_bottom_level_1").css("background-image","url('"+evn_Path+"resource/device_level_1.png')");
		$(".device_use_content_bottom_level_2").css("background-image","url('"+evn_Path+"resource/device_level_2.png')");
		$(".device_use_content_bottom_level_3").css("background-image","url('"+evn_Path+"resource/device_level_3.png')");
		$(".device_use_content_bottom_level_4").css("background-image","url('"+evn_Path+"resource/device_level_4.png')");
		$(".device_use_content_bottom_level_5").css("background-image","");
		$(".device_use_content_bottom_level_6").css("background-image","");
		$(".device_use_content_bottom_level_7").css("background-image","");
		break;
	}
	case 5:{
		$(".device_use_content_bottom_level_1").css("background-image","url('"+evn_Path+"resource/device_level_1.png')");
		$(".device_use_content_bottom_level_2").css("background-image","url('"+evn_Path+"resource/device_level_2.png')");
		$(".device_use_content_bottom_level_3").css("background-image","url('"+evn_Path+"resource/device_level_3.png')");
		$(".device_use_content_bottom_level_4").css("background-image","url('"+evn_Path+"resource/device_level_4.png')");
		$(".device_use_content_bottom_level_5").css("background-image","url('"+evn_Path+"resource/device_level_5.png')");
		$(".device_use_content_bottom_level_6").css("background-image","");
		$(".device_use_content_bottom_level_7").css("background-image","");
		break;
	}
	case 6:{
		$(".device_use_content_bottom_level_1").css("background-image","url('"+evn_Path+"resource/device_level_1.png')");
		$(".device_use_content_bottom_level_2").css("background-image","url('"+evn_Path+"resource/device_level_2.png')");
		$(".device_use_content_bottom_level_3").css("background-image","url('"+evn_Path+"resource/device_level_3.png')");
		$(".device_use_content_bottom_level_4").css("background-image","url('"+evn_Path+"resource/device_level_4.png')");
		$(".device_use_content_bottom_level_5").css("background-image","url('"+evn_Path+"resource/device_level_5.png')");
		$(".device_use_content_bottom_level_6").css("background-image","url('"+evn_Path+"resource/device_level_6.png')");
		$(".device_use_content_bottom_level_7").css("background-image","");
		break;
	}
	case 7:{
		$(".device_use_content_bottom_level_1").css("background-image","url('"+evn_Path+"resource/device_level_1.png')");
		$(".device_use_content_bottom_level_2").css("background-image","url('"+evn_Path+"resource/device_level_2.png')");
		$(".device_use_content_bottom_level_3").css("background-image","url('"+evn_Path+"resource/device_level_3.png')");
		$(".device_use_content_bottom_level_4").css("background-image","url('"+evn_Path+"resource/device_level_4.png')");
		$(".device_use_content_bottom_level_5").css("background-image","url('"+evn_Path+"resource/device_level_5.png')");
		$(".device_use_content_bottom_level_6").css("background-image","url('"+evn_Path+"resource/device_level_6.png')");
		$(".device_use_content_bottom_level_7").css("background-image","url('"+evn_Path+"resource/device_level_7.png')");
		break;
	}
}
}

function openDeviceLevel(level){
	if(currentDevice == 0){
		
		alert("请选择要操作的面板！");
		return;
	}
	$.post(evn_Path+"device!openDevice.do",{'operateType':'turn','level':level,'deviceId':currentDeviceId},
			function(data){
				var a = data.split(",");
				if(a[0]=='success'){
					levelChange(level);
				}
			});
}

function deviceOperate(idName,upDown,operate,type){
	if(currentDevice!=0 || currentRoomId!=0){
		
		if(upDown == 'down'){
			if(type == 'device'){
				if(currentDevice!=0){
					$("#"+idName).css("background-image","url('"+evn_Path+"resource/device_button_press.png')");
				}else{
					alert("请选择要操作的面板！");
				}
			}else{
				$("#"+idName).css("background-image","url('"+evn_Path+"resource/device_button_press.png')");
			}
		}else if(upDown == 'up'){
			if(type == 'device'){
				$.post(evn_Path+"device!openDevice.do",{'operateType':'noTurn','openClose':operate,'deviceId':currentDeviceId},
						function(data){
							var a = data.split(",");
							if(a[0]=='success'){
								if(operate==1){
									if(pressDeviceType==1){
										if(listSpace==2){
											$("#device_use_img_li"+currentDevice).attr("className","device_use_img_li_on_off_open_s");
										}else{
											$("#device_use_img_li"+currentDevice).attr("className","device_use_img_li_on_off_open");
										}
									}else if(pressDeviceType==2){
										if(listSpace==2){
											$("#device_use_img_li"+currentDevice).attr("className","device_use_img_li_turn_open_s");
										}else{
											$("#device_use_img_li"+currentDevice).attr("className","device_use_img_li_turn_open");
										}
									}else if(pressDeviceType==3){
										if(listSpace==2){
											$("#device_use_img_li"+currentDevice).attr("className","device_use_img_li_socket_open_s");
										}else{
											$("#device_use_img_li"+currentDevice).attr("className","device_use_img_li_socket_open");
										}
									}
								}else if(operate==0){
									if(pressDeviceType==1){
										if(listSpace==2){
											$("#device_use_img_li"+currentDevice).attr("className","device_use_img_li_on_off_s");
										}else{
											$("#device_use_img_li"+currentDevice).attr("className","device_use_img_li_on_off");
										}
									}else if(pressDeviceType==2){
										if(listSpace==2){
											$("#device_use_img_li"+currentDevice).attr("className","device_use_img_li_turn_s");
										}else{
											$("#device_use_img_li"+currentDevice).attr("className","device_use_img_li_turn");
										}
									}else if(pressDeviceType==3){
										if(listSpace==2){
											$("#device_use_img_li"+currentDevice).attr("className","device_use_img_li_socket_s");
										}else{
											$("#device_use_img_li"+currentDevice).attr("className","device_use_img_li_socket");
										}
									}
									
								}
								levelChange(parseInt(a[1]));
								currentDevice = 0;
								
							}
							$("#"+idName).css("background-image","url('"+evn_Path+"resource/device_button.png')");
						});
			}else if(type=='room'){
				$.post(evn_Path+"device!openRoom.do",{'roomId':currentRoomId,'openClose':operate},
						function(data){
							if(data=='success'){
								if(operate==1){
									if(listSpace == 1){
									$(".device_use_content_top_right_list ul li").each(
											function(){
												var className = $(this).attr("className");
												if(className.indexOf("device_use_img_li_on_off")!=-1){
													$(this).attr("className","device_use_img_li_on_off_open");
												}else if(className.indexOf("device_use_img_li_turn")!=-1){
													$(this).attr("className","device_use_img_li_turn_open");
												}else if(className.indexOf("device_use_img_li_socket")!=-1){
													$(this).attr("className","device_use_img_li_socket_open");
												}
											}
										);
									}else{
										$(".device_use_space_background div").each(
												function(){
													var className = $(this).attr("className");
													if(className.indexOf("device_use_img_li_on_off")!=-1){
														$(this).attr("className","device_use_img_li_on_off_open_s");
													}else if(className.indexOf("device_use_img_li_turn")!=-1){
														$(this).attr("className","device_use_img_li_turn_open_s");
													}else if(className.indexOf("device_use_img_li_socket")!=-1){
														$(this).attr("className","device_use_img_li_socket_open_s");
													}
												}
											);
									}
								}else if(operate==0){
									if(listSpace == 1){
									$(".device_use_content_top_right_list ul li").each(
											function(){
												var className = $(this).attr("className");
												if(className.indexOf("device_use_img_li_on_off")!=-1){
													$(this).attr("className","device_use_img_li_on_off");
												}else if(className.indexOf("device_use_img_li_turn")!=-1){
													$(this).attr("className","device_use_img_li_turn");
												}else if(className.indexOf("device_use_img_li_socket")!=-1){
													$(this).attr("className","device_use_img_li_socket");
												}
											}
										);
									}else{
										
										$(".device_use_space_background div").each(
												function(){
													var className = $(this).attr("className");
													if(className.indexOf("device_use_img_li_on_off")!=-1){
														$(this).attr("className","device_use_img_li_on_off_s");
													}else if(className.indexOf("device_use_img_li_turn")!=-1){
														$(this).attr("className","device_use_img_li_turn_s");
													}else if(className.indexOf("device_use_img_li_socket")!=-1){
														$(this).attr("className","device_use_img_li_socket_s");
													}
												}
											);
									}
								}
							}
							$("#"+idName).css("background-image","url('"+evn_Path+"resource/device_button.png')");
						});
			}
			$('.main_left_menu_openClose ul li img').attr("src",evn_Path+"resource/main_left_menu_close.png");
			
		}
	}else{
		if(upDown=='down'){
			alert("请选择要操作的面板或房间！");
		}
	}
	
}

function settingDown(posistionClassName,downClassName,overHeight,upDown){
	var top = $("."+posistionClassName).position().top;
	var height = $("."+posistionClassName).height();
	if((top + height) > overHeight){
		if(upDown == 'down'){
			$("."+downClassName).css("background-image","url('"+evn_Path+"images/down_press.png')");
		}
		if(upDown == 'up'){
			
			top-=30;
			$("."+posistionClassName).css("top",top+"px");
			$("."+downClassName).css("background-image","url('"+evn_Path+"images/down.png')");
  	  	}
	}
	  	
}
function settingUp(posistionClassName,downClassName,upDown){
	var top = $("."+posistionClassName).position().top;
	var height = $("."+posistionClassName).height();
	if((top + height) < height){
		if(upDown == 'down'){
			$("."+downClassName).css("background-image","url('"+evn_Path+"images/up_press.png')");
		}
		if(upDown == 'up'){
			
		//	alert("top:" + top + "\r\nheight:"+height);
			top+=30;
			$("."+posistionClassName).css("top",top+"px");
			$("."+downClassName).css("background-image","url('"+evn_Path+"images/up.png')");
  	  	}
	}
	  	
}

var listSpace = 0;


function callVK(){
	$.get("callVK.do");
}